package com.example.yf.result;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Result<T> {
    private int code;
    private String msg;
    private T data;


    public Result(){

    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(ResultCodeEnum resultCodeEnum,T data){
        this.code=resultCodeEnum.getCode();
        this.msg=resultCodeEnum.getMsg();
        this.data=data;
    }

    //根据枚举类创建构造方法
    public Result(int code , String msg) {
        this.code = code;
        this.msg = msg;
    }

    //根据枚举类创建构造方法
    public Result(ResultCodeEnum resultCodeEnum) {
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMsg();
    }

    //success的静态方法，只需要传入返回的数据
    public static Result SUCCESS(Object data) {
        return new Result(ResultCodeEnum.SUCCESS,data);
    }

    //success的静态方法，没有返回参数
    public static  Result SUCCESS(){
        return SUCCESS(null);
    }

    //失败的通用方法
    public static Result ERROR(){
        return ERROR(ResultCodeEnum.ERROR);
    }


    //自定义的code失败方法
    private static Result ERROR(ResultCodeEnum resultCodeEnum) {
        return new Result(resultCodeEnum);
    }


    //失败的通用方法
    public static Result ERROR(int code,String msg){
        return new Result(code,msg);
    }


    public static Result error(int code,String defaultMessage) {
        return new Result(code,defaultMessage);
    }


}
